package com.lrc.springcloud.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.lrc.springcloud.service.PaymentService;
import com.lrc.springcloud.entity.CommonResult;
import com.lrc.springcloud.entity.Payment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @author Administrator
 */
@Api(value = "payment", tags = {"payment provider Api"}, protocols = "http")
@RestController
@RefreshScope
@Slf4j
@RequestMapping("/payment")
public class PaymentController {

    @Value("${devConfig}")
    private String devConfig;

    @Autowired
    private PaymentService paymentService;

    @ApiOperation(
            value = "创建Payment",
            notes = "创建一个Payment",
            produces = "application/json, application/xml",
            consumes = "application/json, application/xml",
            response = CommonResult.class
    )
    @PostMapping(value = "/create")
    public CommonResult create(@ApiParam(name = "payment", value = "payment对象") Payment payment) {
        log.info("************插入：{}", payment);
        Integer integer = paymentService.create(payment);
        log.info("************插入结果：{}", integer);
        if (integer > 0) {
            return new CommonResult<>(200, "操作成功", integer);
        } else {
            return new CommonResult<>(444, "操作失败", null);
        }
    }

    @SentinelResource("hello")
    @GetMapping(value = "/get/{id}")
    public CommonResult getPaymentById(@ApiParam(name = "id", value = "查询Id", required = true) @PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("*******插入结果：{}", payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功", payment);
        }
        return new CommonResult(444, "没有对应记录", new Payment());
    }

    @GetMapping("/hello")
    public String getDevConfig() {
        return devConfig;
    }

}
